package supermarket;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.LogFactory;
import org.dom4j.dom.DOMText;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.html.DomAttr;
import com.gargoylesoftware.htmlunit.html.DomText;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlUnorderedList;

public class ProductScraperDelhaize extends ProductScraper<HtmlPage,HtmlDivision,HtmlPage> {
	private List<HtmlAnchor> productCategoryAnchors;
	private Iterator<HtmlAnchor> productCategoryAnchorsIterator;
	
	private List<HtmlPage> pageWithProducts;
	private Iterator<HtmlPage> pageWithProductsIterator;
	
	private List<HtmlDivision> allProducts;
	private Iterator<HtmlDivision> allProductsIterator;
	
	public static void main(String[] args){
		LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); 
		ProductScraperDelhaize psd = new ProductScraperDelhaize(null);
		psd.scrape();
	}
	
	public ProductScraperDelhaize(ProductManager manager) {
		super(manager);
	}


	@Override
	protected HtmlPage goToInitialPage() {
	    HtmlPage page = null;
		try {
			System.out.println(getMarketURL());
			page = (HtmlPage) getWebClient().getPage(getMarketURL());
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    // form selecteren
//	    HtmlForm form = page.getFormByName("aspnetForm");


		
//	    System.out.println(page.getFormByName("aspnetForm").getRadioButtonsByName("SlotGroup").get(0).isChecked());
	    
//	    final HtmlAnchor button = (HtmlAnchor) page.getElementById("ctl00_ctl00_mainContent_mainColumnContent_linkStart");
////	    final HtmlAnchor button = (HtmlAnchor) page.getAnchorByHref("javascript:__doPostBack('ctl00$ctl00$mainContent$mainColumnContent$linkStart','')");
//	    try {
//			page = button.click();
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//	    
//	    try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	    System.out.println(page.asXml());
		
		return page;

	}

	@Override
	public boolean hasNextScrapeLinks() {
		return pageWithProductsIterator.hasNext();
	}

	@Override
	public HtmlPage getNextScrapeLink() {
		return pageWithProductsIterator.next();
	}

	@Override
	public Distributor getDistributor() {
		return Distributor.DELHAIZE;
	}


	@Override
	public void scrapeProduct(HtmlDivision productToScrape) {       
		
		HtmlDivision product = (HtmlDivision) productToScrape;
		List<HtmlParagraph> productBrandParagraphs = (List<HtmlParagraph>) product.getByXPath("./div[@class='displayProdList']/div[@class='prodListNameDescr']//p[@class='prodListName']");
		String productBrand = "";
		for(int i = 0; i < productBrandParagraphs.size(); i++){
			if(i != 0){
				productBrand += " | ";
			}
			productBrand += productBrandParagraphs.get(i).asText(); 
		}
//	    System.out.println(page.getByXPath("//div[@class='detailWrapper']//a[@class='PB_Product_Brand'][1]/text()").size()); 
	    DomText productName  = (DomText) product.getFirstByXPath("./div[@class='displayProdList']/div[@class='prodListNameDescr']/p[@class='prodListDescrTxt']/a/text()");
//	    DomAttr href  = (DomAttr) product.getFirstByXPath("./div[@class='detailWrapper']//a[@class='PB_Product_Name'][1]/@href");
	    DomAttr inputValue = (DomAttr) product.getFirstByXPath("./input/@value");
	    
	    String productID = inputValue.getValue();
	    String[] productIDparts = productID.split("pid\":\"");
	    productID = productIDparts[1].split("\"")[0];
	    
	    DomText productDisplayPrice = (DomText) product.getFirstByXPath("./div[@class='displayProdList']/div[@class='prodListPriceDescr']/p[@class='prodListPrice']/text()");
	    DomText productDisplayRatio = (DomText) product.getFirstByXPath("./div[@class='displayProdList']/div[@class='prodListPriceDescr']/p[@class='prodListRatio']/text()");
	    System.out.println("*** product with ID " + productID);
	    System.out.println("product name: " + productName.asText());
	    System.out.println("product brand: " + productBrand);
	    System.out.println("productDisplayPrice: " + productDisplayPrice.asText());
	    System.out.println("productDisplayRatio: " + productDisplayRatio.asText());
//	    DomText productWeight = (DomText) product.getFirstByXPath("./div[@class='priceListWrapper']//span[@class='productListPackaging PB_Product_Packaging'][1]/text()");
//	    DomText productBorderPrice = (DomText) product.getFirstByXPath("./div[@class='priceListWrapper']//span[@class='borderPrice'][1]/text()");
//	    writeLine("PID: " + productID + " merk: " + productBrand + " productnaam: " + productName.getValue() + " displayprijs: " + productDisplayPrice + 
//	    		" gewicht: " + productWeight + " prijs per kilo: " + productBorderPrice);
	    
//	    String amount = productDisplayPrice.asText().split(" ")[1];
//	    String currency = productDisplayPrice.asText().split(" ")[0];
//	    
//	    String quantityArray[] = productWeight.asText().split(" ");
//	    String quantity;
//	    String unit;
//	    
//	    if(quantityArray[0].startsWith("+")){
//	    	quantity = quantityArray[1];
//	    	unit = quantityArray[2];
//	    }
//	    else{
//	    	quantity = quantityArray[0];
//	    	unit = quantityArray[1];
//	    }
////	    System.out.println("***pBP***: " + productBorderPrice);
//	    // sommige delhaizeproducten hebben enkel een eenheidsprijs als hoofdprijs staan en dan geeft productBorderPrice null
//	    if(productBorderPrice != null){
//	    	String unitArray[] = productBorderPrice.asText().split(" ");
//	    	String unitPrice = unitArray[0];
//	    	String unitOfUnitPrice = unitArray[1].split("/")[1];
//	    
//	    	getManager().processProductInput(productName.getValue(), productBrand, productID, getDistributor());
//   		    getManager().processProductPriceInput(productID, getDistributor(), amount, currency, quantity, unit, unitPrice, unitOfUnitPrice);
//	    	System.out.println("PID: " + productID + " merk: " + productBrand + " productnaam: " + productName.getValue() + " displayprijs: " + productDisplayPrice + 
//	    			" gewicht: " + productWeight + " prijs per kilo: " + productBorderPrice);
//	    }
	}

	@Override
	public boolean hasNextProduct() {
		return allProductsIterator.hasNext();
	}

	@Override
	public HtmlDivision getNextProduct() {
		return allProductsIterator.next();		
	}

	@Override
	public void getProductCategories(HtmlPage page) {
//	    boolean isFirstLevel = (level == 1);
	    
//	    String levelString = "level_0" + level;
	    // /html/body/form[@name='aspnetForm']/div[@id='container']/div[@class='content']/div[@id='menu']/ul[@class='level_01']
//	    HtmlUnorderedList levelList = page.getFirstByXPath("//ul[@class='" + levelString + "']");
	    //alle items van level_01
	    //TODO levelList geeft soms nullpointer 
//	    levelItems = (List<HtmlListItem>) levelList.getByXPath(".//li");
//	    levelItemsIterator = levelItems.iterator();
	    
	    String levelString = "firstLevel";
//	    List<HtmlDivision> divisions = (List<HtmlDivision>) page.getByXPath("//div[@class='" + levelString + "']/div[@class='subNav']");
	    productCategoryAnchors = (List<HtmlAnchor>) page.getByXPath("//div[@class='" + levelString + "']/div[@class='subNav']//dd/a");
	    for(HtmlAnchor anchor : productCategoryAnchors){
		    System.out.println(anchor.getHrefAttribute());	
	    }

	    System.out.println(productCategoryAnchors.size());

	    productCategoryAnchorsIterator = productCategoryAnchors.iterator();
	}

	@Override
	public HtmlPage processProductCategory(Object item) {
//		HtmlListItem category = (HtmlListItem) item;
	    HtmlAnchor link = (HtmlAnchor) item;
	    HtmlPage pageWithProducts = null;
		try {
//			pageWithProducts = link.click();
			pageWithProducts = getWebClient().getPage("http://www.delhaizedirect.be" + link.getHrefAttribute());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
    	}

		System.out.println(link.getHrefAttribute() + " succesvol gescraped");		
		
		return pageWithProducts;
	}

	@Override
	public void scrapeLink(HtmlPage link) {
		scrapePage(link);		
	}

	@Override
	protected HtmlAnchor getNextProductCategory() {
		return productCategoryAnchorsIterator.next();	
	}

	@Override
	protected boolean hasNextProductCategory() {
		return productCategoryAnchorsIterator.hasNext();
	}

	@Override
	protected void getScrapeLinks(HtmlPage pageWithLinks) {
	
	    pageWithProducts = new ArrayList<HtmlPage>();
	    pageWithProducts.add(pageWithLinks);

	    pageWithProductsIterator = pageWithProducts.iterator();
	}

	@Override
	protected void getProducts(HtmlPage page) {
		
    	String levelString = "mosaiqueList ulProdList";
//    	System.out.println(page.asXml());
    	
	    allProducts = (List<HtmlDivision>) page.getByXPath("//ul[@class='" + levelString + "']/div");
	    allProductsIterator = allProducts.iterator();
		
	}


}
